// pages/index/decorcompany/decorcompany_detail.js
//import WxValidate from '../../../utils/WxValidate'
//var template = require('../public/template_evaluate.js') 
var postData = require('../../../data/post-data.js')
var call = require("../../../utils/request.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clicked: 2,
    clickede: 1,
    clickedes: 1,
    clickedese: 2,
    one_star: '',
    two_star: 1,
    three_star: 0,
    disableds: true,
    start: false,
    afferent: '',
    mobile: '',
    area: '',
    radvalue: 1,
    radname: '',
    main_room: "3室",
    main_tin: "2厅",
    main_wei: "2卫",
    main_yang: "3阳台",
    houstyle: '3223',
    room: 1,
    tin: 1,
    wei: 1,
    yan: 1,
    hidecode: true,
    showcode: false,
    infotele: '',
    telepho: '',
    latitude: 0,
    longitude: 0,
    getlatitude: 0,
    getlongitude: 0,
    chooseradio: '0',
    tid: '',
    scence: '',
    polistde: '',
    linner: '附近有29家这样的',
    backg: 'https://img.xtuan.com/xcximgtest/images/banner_02.jpg',
    imgurl: "",
    imgurls: '',
    tocken: '',
    city: '',
    cityid: '',
    proviceid: '',
    cards: true,
    card: '获取验证码',
    downqcode: '',
    company: '',
    land_page: '',
    sid: '',
    landing_page: '',
    basepeak: '',
    star: '',
    companyID: '',
    colist: [{
      name: '人工费',
      price: '?'
    },
    {
      name: '材料费',
      price: '?'
    },
    {
      name: '设计费',
      price: '?'
    },
    {
      name: '质检费',
      price: '?'
    }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opctions) {
    var that = this;
    var id = opctions.id;
    var latitude = that.data.latitude
    var longitude = that.data.longitude
    var getlatitude = that.data.getlatitude
    var getlongitude = that.data.getlongitude
    var decordetail = postData.pdecormpany[id];
    //var scene = decodeURIComponent(opctions.id);
    var companyID = opctions.companyID
    var scene = decodeURIComponent(opctions.scene)
    var query = opctions.id
    if (typeof (opctions.scene) != "undefined") {
      var id = decodeURIComponent(opctions.scene)
    } else {
      var id = opctions.id
    }
    wx.request({
      url: app.apiUrl("c=zhuangxiu/index&a=getdetail"),
      data: {
        companyID: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var polist = res.data.data
        var telephone = polist.telephone
        var company = polist.companyName
        var latitude = polist.latitude
        var longitude = polist.longitude
        var star = parseInt(polist.compose)
        var width = star * 5.4 + "%"
        var half = 5 - star
        if (half > 0.5) {
          half = 1
        }
        if ((polist.latitude == null) || (polist.longitude == null)) {
          polist.state = false
        } else {
          polist.state = true
        }
        for (var i in polist.rend) {
          var style = polist.rend[i].styleValue
          if (style == '') {
            polist.rend[i].statue = false
          } else {
            polist.rend[i].statue = true
          }
        }
       //console.log(polist.rend)
        that.setData({
          showdistance: false,
          polistde: polist,
          telepho: telephone,
          getlatitude: latitude,
          getlongitude: longitude,
          tid: id,
          company: company,
          star: star,
          half: half,
          showstar: width,
          //imglen: imgth
        })

        if (polist.rend.length == 0) {
          that.setData({
            showcase: false,
            bottom: '100rpx',
            mapbtom: '100rpx'
          })
        } else {
          that.setData({
            showcase: true,
            bottom: 0,
            mapbtom: 0
          })
        }
        //console.log('获取的经纬度为', latitude)
        //console.log(res.data)
        wx.setNavigationBarTitle({
          title: company //页面标题为路由参数
        })
        that.downQcode()
        setTimeout(function () {
          that.distance()
        }, 200)
      }
    })
    that.setData({
      // postlist: postData.postlist,
      companyID: id
    })
    that.getlocal()
    // setTimeout(function() {
    //   that.downQcode()
    // }, 1000)
    //that.getevaluate()
    setTimeout(function () {
      that.initcode()
      that.getmap()
    }, 200)
    // that.getmap()
    that.basepeak()
    call.getData('session', this.shuffleSuc, this.fail);
    call.getOrigin('origin', this.origisucess, this.originfail);
  },
  //获取session
  shuffleSuc: function (res) {
    //console.log(res)
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      sid: sid
    })

  },
  fail: function () {
    console.log("失败")
  },
  //获取页面来源
  origisucess: function (res) {
    var that = this;
    //console.log('请求成功', res)
    var land_page = res.datas.landing_page
    var housetype = res.datas.house_type
    var decorate = res.datas.decorate_type
    that.setData({
      landing_page: land_page,
      housetype: housetype,
      decore: decorate
    })

  },
  originfail: function () {
    console.log("失败")
  },

  //签约页面来源页信息
  basepeak: function () {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=view_bespeak_company'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: "GET",
      success: function (res) {
        //console.log('请求成功', res.data)
        var landing_page = res.data.datas.landing_page
        //console.log('奥术大师', landing_page)
        that.setData({
          basepeak: landing_page
        })
      },
      fail: function (res) {
        console.log('请求失败', res.data)
      }
    })
  },
  //获取当前经纬度
  getlocal: function (e) {
    var that = this
    var count = that.data.linner
    var getlatitude = that.data.getlatitude
    var getlongitude = that.data.getlongitude
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.setStorageSync('latitude', latitude)
        wx.setStorageSync('longitude', longitude)
        var speed = res.speed
        var accuracy = res.accuracy
        that.setData({
          latitude: latitude,
          longitude: longitude,
        })
      }
    })
  },
  //获取附近地图数据
  getmap: function () {
    var that = this
    var id = that.data.companyID
    var la1 = wx.getStorageSync('latitude');
    var lo1 = wx.getStorageSync('longitude');
    var lati = that.data.latitude
    var longi = that.data.longitude
    if (!(la1 == lati) || !(lo1 == longi)) {
      that.getlocal();
    }
    //console.log(la1,lo1)
    wx.request({
      url: app.apiUrl('c=buildingsite/index&a=get_map_data'),
      data: { company_id: id },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: "POST",
      success: function (res) {
        console.log('请求成功', res.data)
        var nums = res.data.datas.num
        var maplist = res.data.datas.list[0]
        if (res.data.datas.list.length == 0) {
          that.setData({
            exhibition: false
          })
        }
        else {
          var long = maplist.location
          //console.log('获取的',long)
          if (long == null || long == undefined) {
            that.setData({
              neardistance: false
            })
          } else {
            var longs = []
            longs = long.split(",");
            var la2 = longs[1]
            var lo2 = longs[0]
            var La1 = la1 * Math.PI / 180.0;
            var La2 = la2 * Math.PI / 180.0;
            //console.log(la2)
            var La3 = La1 - La2;
            var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
            var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
            s = s * 6378.137; //地球半径
            s = parseInt(Math.round(s * 10000) / 10000);
            that.setData({
              getlatitudes: la2,
              getlongitudes: lo2,
              numbers: nums,
              near: s,
              neardistance: true
            })
            //console.log(s)
          }
          that.setData({
            maplist: maplist,
            exhibition: true
          })
        }
        that.setData({
          numbers: nums
        })
      },
      fail: function (res) {
        console.log('请求失败', res.data)
      }
    })
  },
  //下载二维码图片
  downQcode: function () {
    var that = this
    var id = that.data.tid
    // console.log('公司的id是', id)
    wx.downloadFile({
      url: app.apiUrl('c=zhuangxiu/index&a=getqrcode&companyID=' + id),
      success: function (res) {
        if (res.statusCode === 200) {
          that.setData({
            downqcode: res.tempFilePath
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  //小程序分享
  onShareAppMessage: function (res) {
    var imgurl = this.data.imgurls
    var id = this.data.tid
    var title = this.data.company
    wx.showShareMenu({
      withShareTicket: true
    })
    return {
      title: title,
      path: 'pages/index/decorcompany/decorcompany_detail?id=' + id,
      //imageUrl: base64Image,
      success: function (res) {
        // 转发成功
        // wx.previewImage({
        //   current: canvasUrl, // 当前显示图片的http链接
        //   urls: arr // 需要预览的图片http链接列表
        // })
      },
      fail: function (res) {
        // 转发失败
      }
    }
    console.log(url)
  },

  //初始获取验证码
  initcode: function () {
    var that = this
    var sid = that.data.sid
    //console.log('奥术大师', sid)
    var text = '获取验证码'
    wx.request({
      url: app.apiUrl("c=zhaobiao/index&a=get_captcha"),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: 'GET',
      success: function (res) {
        //console.log(res.data)
        var code = res.data.datas.code
        that.setData({
          card: code,
          hidecode: false,
          showcode: true,
        })
        setTimeout(function () {
          that.setData({
            card: text,
          })
        }, 30000)
      }
    })
  },
  //转发
  gencard: function (e) {
    var that = this
    var downqcode = that.data.downqcode
    var arr = []
    arr.push(downqcode)
    wx.previewImage({
      current: downqcode, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  //距离计算
  distance: function (e) {
    var that = this
    var la1 = wx.getStorageSync('latitude');
    var lo1 = wx.getStorageSync('longitude');
    var lati = that.data.latitude
    var longi = that.data.longitude
    if (!(la1 == lati) || !(lo1 == longi)) {
      that.getlocal();
    }
    var la2 = that.data.getlatitude
    var lo2 = that.data.getlongitude
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    //console.log(la2)
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = parseInt(Math.round(s * 10000) / 10000);
    that.setData({
      distance: s
    })
  },


  // 截获竖向滑动
  catchTouchMove: function (res) {
    return false
  },

  //到装修攻略
  skipst: function () {
    var id = this.data.tid
    console.log('公司id是', id)
    wx.navigateTo({
      url: '../temporary/strategy?id=' + id
    })
  },
  //显示蒙版
  chooroom: function () {
    var that = this;
    this.setData({
      showModal: true,
      Modalcontent: true
    })
  },
  //弹框隐藏
  confirm: function () {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false

    })
  },
  hidemask: function (e) {
    this.setData({
      showModal: false,
      Modalcontent: false,
      showinfo: false
    })
    console.log(1232)
  },
  cancle: function () {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false
    })
  },
  //房屋类型弹框内容点击选择
  choose_btn: function (e) {
    var that = this;
    var room = e.target.dataset.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''

    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].room;
    if (this.data.clicked == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clicked: e.currentTarget.id,
      main_room: value,
      room: room,
      houstyle: housty
    })
    console.log(e)
  },

  choose_tin: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].hall;
    var room = that.data.room
    var tin = e.target.dataset.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickede == e.currentTarget.id) {
      this.setData({
        clickIde: +1,
      })
      return;
    }
    that.setData({
      clickede: e.currentTarget.id,
      main_tin: value,
      tin: tin,
      houstyle: housty
    })
    console.log(value)
  },

  choose_wei: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].toilet;
    var room = that.data.room
    var tin = that.data.tin
    var wei = e.target.dataset.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        clickIdes: +1,
      })
      return;
    }
    that.setData({
      clickedes: e.currentTarget.id,
      main_wei: value,
      wei: wei,
      houstyle: housty
    })
    console.log(value)
  },

  choose_sun: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].balcony;
    var room = that.data.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = e.target.dataset.tty
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickedese == e.currentTarget.id) {
      this.setData({
        clickIdese: +1,
      })
      return;
    }
    that.setData({
      clickedese: e.currentTarget.id,
      main_yang: value,
      yan: yan,
      houstyle: housty
    })
    console.log(value)
  },
  //面积，装修方式，手机号码获取
  radioChange: function (e) {
    var radvalue = this.data.radvalue
    var values = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        chooseradio: +1,
      })
      return;
    }
    this.setData({
      radvalue: values,
      chooseradio: e.currentTarget.id,
    })
    console.log(values)
  },
  givearea: function (e) {
    var value = e.detail.value
    this.setData({
      area: value
    })
  },
  givetele: function (e) {
    var value = e.detail.value
    this.setData({
      mobile: value
    })
  },
  //对话隐藏
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //form报价表单提交
  skipsrsult: function (e) {
    var that = this
    var arr = []
    var city = that.data.afferent
    var alea = that.data.area
    var mobile = that.data.mobile
    var radios = that.data.radvalue
    var mainroom = that.data.main_room
    var ting = that.data.main_tin
    var wei = that.data.main_wei
    var yang = that.data.main_yang
    var houstyle = that.data.houstyle
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var sid = that.data.sid
    var landing_page = that.data.landing_page
    console.log("z这个sid是", sid)
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log(mobile)
    if (city == '') {
      that.setData({
        alertmess: true,
        message: '请选择城市！'
      })
      that.settime()
    } else if (alea == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (radios == '') {
      that.setData({
        alertmess: true,
        message: '装修类型未选择！'
      })
      that.settime()
    } else if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '请填写手机号码！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号码有误,请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl("c=zhaobiao/index&a=submit_estimate"),
        data: {
          phone: mobile,
          acreage: alea,
          house_type: houstyle,
          decorate_type: radios,
          province: proviceid,
          city: cityid,
          landing_page: landing_page
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: 'POST',
        success: function (res) {
          console.log(res.data)
          var code = res.data.code
          var error = res.data.error
          if (code == 200) {
            var bid_id = res.data.datas.bid_id
            var unid = res.data.datas.uuid
            var error = res.data.error
            console.log('这个bind_id为', bid_id)
            var id = 5
            wx.navigateTo({
              url: '../free/quote_result?data=' + [id, alea, radios, mainroom, ting, wei, yang, city, mobile, bid_id, unid]
            })
          } else if (code == 400) {
            that.setData({
              alertmess: true,
              message: error
            })
            that.settime()
          }
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
    }
  },


  //按钮输入
  bindKeyInput: function (e) {
    var value = e.detail.value
    this.setData({
      inputValue: e.detail.value,
      modelclear: true,
      disableds: false,
      start: true,
    })
  },
  clearboth: function (e) {
    this.setData({
      mobile: '',
      modelclear: false,
      disableds: true,
      start: false,
    })
  },
  //专项获活动的显示和隐藏
  skip_active: function (e) {
    this.setData({
      active: true,
      showModal: true
    })
  },
  quit: function (e) {
    var that = this
    that.setData({
      active: false,
      showModal: false
    })
  },
  //跳转到效果图
  binddetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../../impredra/impredra_detail?id=" + id
    })
    //console.log(id);
  },
  //到当前公司的效果图
  materscience: function (e) {
    var that = this
    var comid = that.data.tid
    var company = that.data.company
    wx.navigateTo({
      url: "../temporary/impredra?data=" + [comid, company]  //材料商城点击
    })
  },
  skipToindetail: function () {
    wx.navigateTo({
      url: '../../logs/logs',
    })
  },
  /**
   * 打电话
   */
  tell: function () {
    var tele = this.data.telepho
    wx.makePhoneCall({
      phoneNumber: tele //仅为示例，并非真实的电话号码
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  //到用户地图
  skipmaps: function (e) {
    var that = this
    var getlatitude = that.data.getlatitude
    var getlongitude = that.data.getlongitude
    var company = that.data.company
    wx.navigateTo({
      url: 'maps?local=' + [getlatitude, getlongitude, company],
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //到工地直播
  skiplivesite: function (e) {
    var that = this
    var id = that.data.companyID
    //console.log(id)
    // that.setData({
    //   alertmess: true,
    //   message: '尽情期待！'
    // })
    // that.settime()
    wx.navigateTo({
      url: 'livesite?id=' + id,
    })
  },
  //到风水页面
  skipgeomantic: function (e) {
    wx.navigateTo({
      url: 'geomantic'
    })
  },

  //预约参观弹框显示
  appoint: function (e) {
    this.setData({
      showModal: true,
      showinfo: true
    })
  },
  bindmask: function () {
    this.setData({
      showinfo: false,
      showModal: false,
      Modalcontent: false
    })
  },
  //预约输入判断
  bindKeyInput: function (e) {
    var value = e.detail.value
    var info = 1
    this.setData({
      info: info,
      hidecode: false,
      showcode: true,
      infotele: value
    })
  },
  //地址选择
  choosecity: function (e) {
    var id = 5
    var city = this.data.afferent
    wx.navigateTo({
      url: '../public/custom/cucity',
    })
  },

  //获取验证码
  getCode: function (e) {
    var that = this
    var text = '重新获取'
    var tele = that.data.infotele
    var card = that.data.card
    var sid = that.data.sid
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=get_captcha'),
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "GET",
        success: function (res) {
          console.log(res.data)
          var cards = res.data.datas.code
          //var landing_page = res.data.datas.landing_page
          console.log(cards)
          that.setData({
            card: cards,
            //land_page: landing_page,
            distate: false
          })
          setTimeout(function () {
            that.setData({
              card: text,
              distate: false
            })
          }, 60000)
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
    }
  },
  //表单提交
  formSubmit: function (e) {
    var that = this
    var mobile = e.detail.value.mobile
    var code = e.detail.value.code
    var id = that.data.tid

    var thecode = that.data.card
    var land_page = that.data.land_page
    var sid = that.data.sid
    var basepeak = that.data.basepeak
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log('提交', sid)
    console.log(thecode)
    var id = 1
    var title = '预约成功'
    var tsmall = '"恭喜您，预约成功，我们将尽快与您联系!'
    if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '手机号码不能为空！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else if (code == '') {
      that.setData({
        alertmess: true,
        message: '验证码不能为空！'
      })
      that.settime()
    } else if (code != thecode) {
      that.setData({
        alertmess: true,
        message: '验证码不对，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_bespeak_company'),
        data: {
          phone: mobile,
          code: code,
          company_id: id,
          landing_page: basepeak
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "POST",
        success: function (res) {
          console.log(res.data)
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx.navigateTo({
              url: '../free/applysuccess?data=' + [id, title, tsmall],
            })
          } else {
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
      this.setData({
        showModal: false,
        Modalcontent: false,
        showinfo: false
      })
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  }


})